// import { login, logout, getInfo } from '@/api/user'
import {
    getAuth,
    setAuth,
    removeAuth
} from '@/utils/auth';
import { getRoutes,getBtn,getPrintBk } from '@/api/user';
import router, {
    resetRouter
} from '@/router'

const state = {
    token: getAuth('T'),
    name: '',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    introduction: '',
    roles: [getAuth('R')],
    roleId: getAuth('ID') || null,
}

const mutations = {
    SET_TOKEN: (state, token) => {
        state.token = token;
        setAuth('T', token);
    },
    SET_INTRODUCTION: (state, introduction) => {
        state.introduction = introduction
    },
    SET_NAME: (state, name) => {
        state.name = name
    },
    SET_AVATAR: (state, avatar) => {
        state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
        state.roles = roles;
        setAuth('R', roles[0]);
    },
    SET_ROLEID: (state, roleId) => {
        state.roleId = roleId;
        setAuth('ID', roleId);
    },
}

const actions = {
    // user login
    login({
        commit
    }, userInfo) {
        const {
            token,
            userType,
            roleId,
            username
        } = userInfo
        commit('SET_TOKEN', token)
        commit('SET_ROLES', [userType]);
        commit('SET_ROLEID', roleId);
        commit('SET_NAME', username);
    },
    getUserMsg({ commit, state }){
        return new Promise((resolve, reject) => {
            getPrintBk().then((data) => {
                if (data.status) {
                    resolve(data.data)
                } else {
                    reject(data)
                }
            }).catch(error => {
                reject(error)
            })
        })
    },
    // get user author
    getAuthor({ commit, state }) {
        let { roleId } = state;
        return new Promise((resolve, reject) => {
            getRoutes({ k: roleId }).then((data) => {
                if (data.status) {
                    resolve(data.data)
                } else {
                    reject(data)
                }
            }).catch(error => {
                reject(error)
            })
        })
    },
    getAllBtn({ commit, state },roleId) {
        //let { roleId } = state;
        return new Promise((resolve, reject) => {
            getBtn({ roleId: roleId }).then((data) => {
                if (data.status) {
                    resolve(data.data)
                } else {
                    reject(data)
                }
            }).catch(error => {
                reject(error)
            })
        })
    },
    // user logout
    logout({
        commit,
        state,
        dispatch
    }) {
        return new Promise((resolve, reject) => {
            removeAuth()
            commit('SET_TOKEN', '');
            dispatch('tagsView/delAllViews', null, {
                root: true
            })
            resolve();
            reject();
        })
    },

    // remove token
    resetToken({
        commit
    }) {
        return new Promise(resolve => {
            commit('SET_TOKEN', '')
            commit('SET_ROLES', [])
            removeAuth()
            resolve()
        })
    },

    // dynamically modify permissions
    changeRoles({
        commit,
        dispatch
    }, role) {
        return new Promise(async resolve => {
            const token = role + '-token'

            commit('SET_TOKEN', token)
            setAuth('T', token)

            const {
                roles
            } = await dispatch('getInfo')

            resetRouter()

            // generate accessible routes map based on roles
            const accessRoutes = await dispatch('permission/generateRoutes', roles, {
                root: true
            })

            // dynamically add accessible routes
            router.addRoutes(accessRoutes)

            // reset visited views and cached views
            dispatch('tagsView/delAllViews', null, {
                root: true
            })

            resolve()
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
